<template>
  <div class="parkingContainer">
    <div class="search">
      <el-input v-model.trim="searchValue" placeholder="请输入书名、作者、状态关键字进行查询" clearable :prefix-icon="Search" @keyup="getParkingList(searchValue)">
        <template #append>
          <el-button :icon="Search" @click="getParkingList(searchValue)"/>
        </template>
      </el-input>
    </div>
    <el-table :data="parkingData" stripe style="width: 100%;margin-top: 10px;" height="50%">
    <el-table-column prop="bookId" label="回收ID" width="120"/>
    <el-table-column prop="name" label="书名" width="150"/>
    <el-table-column prop="author" label="作者" width="200"/>
    <el-table-column label="图片">
      <template #default="{row}">
        <img :src="row.picture" alt="" style="width: 50px;height: 50px" />
      </template>
    </el-table-column>
    <el-table-column prop="statusName" label="状态" width="120"/>
    <el-table-column prop="place" label="地址" width="300"/>
    <el-table-column prop="createTime" label="发布时间" width="200"  :formatter="formatDate" />
    <el-table-column fixed="right" label="操作" width="100">
      <template #default="row">
          <el-tooltip content="收货" placement="bottom"  v-if="row.row.statusCode === '4'">
            <el-button link type="primary"  size="small" :icon="Edit"  class="myLink"  @click="clickEditHandler(row)" >收货
            </el-button>
          </el-tooltip>
          <el-tooltip content="上架" placement="bottom"  v-if="row.row.statusCode === '2'">
            <el-button link type="primary" size="small" :icon="Edit"  class="myLink"  @click="clickShelveHandler(row)" >上架
            </el-button>
          </el-tooltip>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="shelveBookVisible" title="上架书籍" width="30%" draggable>
        <el-form v-model="shelveBook">
          <el-form-item prop="书籍主键" label="书籍主键">
            <el-input  v-model="shelveBook.bookId" disabled />
          </el-form-item>
          <el-form-item prop="新旧程度" label="新旧程度">
            <el-input  v-model="shelveBook.newCondition" type="number" :min="1" :max="10" placeholder="请输入新旧程度" />
          </el-form-item>
          <el-form-item prop="积分数量" label="积分数量">
            <el-input  v-model="shelveBook.points" type="number" :min="0"  placeholder="请输入可兑换积分数量" />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="shelveBookVisible = false">取消</el-button>
            <el-button type="primary" @click="addBook()">上架</el-button>
          </span>
        </template>
      </el-dialog>
   <div class="pagination">
    <el-pagination v-model:current-page="page" v-model:page-size="size" :page-sizes="[5,10,20,30]" :small="small" :disabled="disabled"
      :background="background" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
  </div>
  </div>
</template>

<script setup>
import { reactive , inject , onMounted , ref } from "vue"
import axios from '../../axios/axiosInstance'
import { ElLoading,ElMessage } from 'element-plus'
import {Search} from '@element-plus/icons-vue'
import { timePanelSharedProps } from "element-plus/es/components/time-picker/src/props/shared"

//变量定义
let searchValue = ref()
let parkingData = reactive([])
let shelveBookVisible = ref(false)
const shelveBook = reactive({
  bookId: "",
  newCondition: 0,
  points: 0,
})
const total = ref(0)
let page = 1
let size = 5
const small = ref(false)
const background = ref(false)
const disabled = ref(false)

//方法

const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  size = val
  init()
}


const formatDate = (row, column, cellValue)=> {
       if (cellValue) {
         const date = new Date(cellValue);
         return date.toLocaleString(); // 根据需求调整格式
       }
       return '';
     }
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  page = val
  init()
}

const clear = (value) => {
    Object.keys(value).map(key => {
      delete value[key]
    })
  }

const filterHandler = (value,row,column) => {
  const property = column['property']
  return row[property] === value
}

const clickShelveHandler = (row) =>{
  shelveBookVisible.value = true;
  shelveBook.bookId = row.row.bookId;
}

const addBook = () => {
  axios.post('/book/shelve/'+shelveBook.bookId+"/"+shelveBook.newCondition+"/"+shelveBook.points).then((res) => {
        console.log("点击上架按钮"+res)
        if(res.code == 200){
          ElMessage({type: "success",message: "上架成功"});
          shelveBookVisible.value = false;
          init()
        }else{
          ElMessage({type: "error",message: "上架失败"});
       }
      }).catch((err) => {
        console.log(err)
      })
}

const clickEditHandler = (row) => {
  console.log("删除的row为：")
  console.log(row)
  let bookId = row.row.bookId
  axios.post('/book/confirmBook/'+bookId).then((res) => {
        console.log("点击收货按钮"+res)
        if(res.code == 200){
          ElMessage({type: "success",message: "收货成功"});
          collegeData.pop(row.$index)
          init()
        }else{
          ElMessage({type: "error",message: "收货失败"});
       }
      }).catch((err) => {
        console.log(err)
      })
}

const filterState = (value, row) => {
  return row.parkingState === value
}

const getParkingList = (searchValue) => {
  if(searchValue != ""){ 
    axios.post(`/book/publishQuery`,{
      page: page,
      size: size,
      search: searchValue,
      chooseReuse: 1,
    }).then((res) => {   
      console.log("res为：")
      console.log(res)
       if(res.code == 200){
          clear(parkingData)
          total.value = res.data.total;
          for(let i in res.data.records){
            console.log(i)
            parkingData.push(res.data.records[i])
          }
          ElMessage({type: "success",message: "查询成功"});
        }else{
          ElMessage({type: "error",message: "查询失败"});
       }
    }).catch((err) => {
      console.log(err)
    })
  }else{
    init()
  }
}

const init = () => {
  const loading = ElLoading.service({
        lock: true,
        text: '拼命加载中',
        background: 'rgba(0, 0, 0, 0.7)',
      })
      setTimeout(() => {
        loading.close()
      }, 2000)
      axios.post(`/book/publishQuery`,{
      page: page,
      size: size,
      chooseReuse: 1,
    }).then((res) => {   
      console.log("res为：")
      console.log(res)
      clear(parkingData)
      total.value = res.data.total
      console.log(total)
      for(let i in res.data.records){
        console.log(i)
        parkingData.push(res.data.records[i])
      }
      console.log("最后的parkingData是：")
      console.log(parkingData)
    }).catch((err) => {
      console.log(err)

    })
}

// const getNameFilter = () => {
//    axios.get('/parking-service/area/areaName').then((res) => {   
//       console.log("res为：")
//       console.log(res)
//       console.log(total)
//       for(let i in res.data){
//         console.log(i)
//         nameFilter.push(res.data[i])
//       }
//       console.log("最后的parkingData是：")
//       console.log(nameFilter)
//     }).catch((err) => {

//       console.log(err)

//     })
// }

//页面初始化方法
onMounted(()=>{ 
  init()
  // getNameFilter()
})
</script>

<style scoped>
.parkingContainer .search{
  display: flex;
  width: 600px;
}
.parkingContainer .pagination{
  justify-content: center;
 margin-top: 15px;
}
.myLink {
  /* 确保没有隐藏样式 */
  display: inline-block;
  visibility: visible;
}


</style>
